import hashlib
import time
import pymysql


def insert_data(username,pw):
    config = {
        "db": "snkr",
        "ip": "106.55.41.183",
        "port": "3306",
        "user": "root",
        "passwd": "pj20211112"
    }
    db = pymysql.connect(
        host=config["ip"],
        port=3306,
        user="root",
        password=config["passwd"],
        database="snkr"
    )
    cur=db.cursor()
    current_time=int(time.time())
    sql="""SELECT * from user_info where user_name='%s'"""%(username)
    cur.execute(sql)
    result=cur.fetchone()
    print(result)
    if result is None:
        sql="""INSERT INTO user_info(user_name,password_hash,ctime,mtime,deleted)  VALUES('%s','%s','%s','%s',0);"""%(username,str(hashlib.sha256(pw.encode("utf-8")).hexdigest()),current_time,current_time)
        print(sql)
        cur.execute(sql)
        db.commit()
    else:
        print("user existed")

if __name__ == '__main__':
    insert_data("lc","123456")